<template>
  <div>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="书名">
        <el-input v-model="form.bookname"></el-input>
      </el-form-item>
      <el-form-item label="作者">
        <el-input v-model="form.author"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit('form')">添加</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>

  </div>

</template>

<script>
export default {
  data() {
    return {
      form: {
        bookname: '',
        author: '',
      }
    }
  },
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let _this = this
          axios.put('http://localhost:8888/books/add',this.form).then(function (response) {
            if(response.data){
              _this.$alert(_this.form.bookname+'添加成功！', '添加数据', {
                confirmButtonText: '确定',
                callback: action => {
                  //跳转到/table
                  _this.$router.push('/list')
                }
              });
            }
          })
        }
      });
    }
  }
}
</script>